//
//  NotBoundUserVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/13.
//  Copyright © 2020 tym. All rights reserved.
//

#import "SeleFamilyUserVC.h"
#import "GroupShadowTableView.h"
#import "Sizes.h"
#import "OpenlimitsVC.h"
#import "FamilyUserCell.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "EquipSettingVC.h"
#import "EquipmentService.h"

@interface SeleFamilyUserVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;
@property(nonatomic,strong) EquipmentService *service;

@property(nonatomic,strong) EquipmentModel *equipment;

@property(nonatomic,strong) NSMutableArray *users;
@end

@implementation SeleFamilyUserVC

-(instancetype)initEquipmentModel:(EquipmentModel*)model{
    self = [super init];
    self.equipment = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"选择家庭成员"];
    [self setTable];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getEquipmentUsers];
}

-(NSMutableArray*)users{
    if (_users == nil) {
        _users = [[NSMutableArray alloc] init];
    }
    return _users;
}

-(EquipmentService*)service{
    if (_service == nil) {
        _service = [[EquipmentService alloc] init];
    }
    return _service;
}

-(void)getEquipmentUsers{
    TYMWeak(self);
    [self.service getEquipmentuseruserall:_equipment.equipmentId familyId:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull respons, NSArray<UserModel *> * _Nonnull users) {
        if (weakself.users.count) {
            [weakself.users removeAllObjects];
        }
        [weakself.users addObjectsFromArray:users];
        [weakself.tableView reloadData];
    }];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeBottomMargin-15-SafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.headTitles = @[@"家庭成员"];
    self.tableView.isShowArray = @[@(1)];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _users.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [FamilyUserCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FamilyUserCell *cell = [tableView dequeueReusableCellWithIdentifier:[FamilyUserCell identifier]];
    if (!cell) {
        cell = [[FamilyUserCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[FamilyUserCell identifier]];
    }
    cell.userModel = self.users[indexPath.row];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OpenlimitsVC *settingVC = [[OpenlimitsVC alloc] initWithUserModel:self.users[indexPath.row] equipMent:self.equipment];
    [self.navigationController pushViewController:settingVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
